package ch01.GridCollision {
	/**
	 * 使用GridCollision,网格检测例子二
	 * @author jianping.shenjp
	 */
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.utils.getTimer;
	import flash.display.DisplayObject;
	import tools.Ball;

	public class GridCollision2 extends Sprite {
		private const GRID_SIZE:Number = 80;
		private const RADIUS:Number = 25;
		private var _balls:Vector.<DisplayObject>;
		private var _grid:CollisionGrid;
		private var _numBalls:int =100;
		private var _numChecks:int = 0;

		public function GridCollision2(){
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			_grid = new CollisionGrid(stage.stageWidth, stage.stageHeight, GRID_SIZE);
			_grid.drawGrid(graphics);
			makeBalls();
			var startTime:int;
			var elapsed:int;
			startTime = getTimer();
			for (var i:int = 0; i < 10; i++){
				_grid.check(_balls);
				var checks:Vector.<DisplayObject> = _grid.checks;
				var numChecks:int = checks.length-1;
				for (var j:int = 0; j < numChecks; j += 1){
					checkCollision(checks[j] as Ball, checks[j + 1] as Ball);
				}
			}
			elapsed = getTimer() - startTime;
			//这儿测试的结果，速度比起之前的几乎快了一倍。这得归功于使用了vector 以及仅创建需要的网格（懒惰实例化）。
			trace("3:Elapsed:", elapsed, "测试次数:", _numChecks);
		}

		private function makeBalls():void {
			_balls = new Vector.<DisplayObject>(_numBalls);
			for (var i:int = 0; i < _numBalls; i++){
				var ball:Ball = new Ball(RADIUS);
				ball.x = Math.random() * stage.stageWidth;
				ball.y = Math.random() * stage.stageHeight;
				addChild(ball);
				_balls[i] = ball;
			}
		}

		private function checkCollision(ballA:Ball, ballB:Ball):void {
			_numChecks++;
			var dx:Number = ballB.x - ballA.x;
			var dy:Number = ballB.y - ballA.y;
			var dist:Number = Math.sqrt(dx * dx + dy * dy);
			if (dist < ballA.radius + ballB.radius){
				ballA.color = 0xFF0000;
				ballB.color = 0xFF0000;
			}
		}
	}
}